<template>
<view class="body">
    <form class="calculator" @submit="submit">
        <view class="base">
            <view class="border">
                <view class="title">所在城市</view>
                <view class="left">
                    <view class="city">
                        <picker name="city" mode="selector" :value="cityIndex" :range="city" @change="citySelect">
                            <view class="content">{{city[cityIndex]}}</view>
                        </picker>
                    </view>
                    <image src="/static/images/calculator/1.png" class="back"></image>
                </view>
            </view>
            <view class="border">
                <view class="title">参保类型</view>
                <view class="left">
                    <view class="type">
                        <picker name="type" mode="selector" :value="typeIndex" :range="type" @change="typeSelect">
                            <view class="content">{{type[typeIndex]}}</view>
                        </picker>
                    </view>
                    <image src="/static/images/calculator/1.png" class="back"></image>
                </view>
            </view>
            <view class="border">
                <view class="title">参保基数</view>
                <view class="left">
                    <view class="base-num">
                        <input name="baseNum" :disabled="isMinNum" @blur="baseNumInput" data-type="base" :value="minNum" type="number" :placeholder="'填写范围：' + (shebaoMin + '-' + shebaoMax)">
                        <text>元</text>
                    </view>
                </view>
            </view>
            <view class="border">
                <view class="title">最低基数</view>
                <view class="left">
                    <view class="min-num">
                        <switch type="switch" name="minNum" color="#FF6600" @change="minBase"></switch>
                    </view>
                </view>
            </view>
        </view>
        <view class="other">
            <view class="border">
                <view class="title">缴纳公积金</view>
                <view class="left">
                    <view class="payment">
                        <switch name="payment" type="switch" @change="switchGjj" color="#FF6600"></switch>
                    </view>
                </view>
            </view>
            <view class="gjj" v-if="gjjSwitch">
                <view class="border">
                    <view class="title">缴存基数</view>
                    <view class="left">
                        <view class="base-num">
                            <input name="payBaseNum" :disabled="isPayMinNum" @blur="baseNumInput" data-type="pay" :value="payMinNum" type="number" :placeholder="'填写范围：' + (gongjijinMin + '-' + gongjijinMax)">
                            <text>元</text>
                        </view>
                    </view>
                </view>
                <view class="border">
                    <view class="title">最低基数</view>
                    <view class="left">
                        <view class="min-num">
                            <switch name="payMinNum" type="switch|checkbox" color="#FF6600" @change="minBase" data-type="pay"></switch>
                        </view>
                    </view>
                </view>
                <view class="border">
                    <view class="title">个人缴存比例</view>
                    <view class="left">
                        <view class="proportion base-num">
                            <input name="proportion" :value="proportion" @blur="baseNumInput" type="number" placeholder="5-12" placeholder-class="input-placeholder">
                            <text>%</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="control">
            <navigator class="to-major" target url="/pages/tools/calculator/major/major" hover-class="navigator-hover" open-type="navigate">
                <image src="/static/images/calculator/4.png" class="tips"></image>
                <text>快去使用五险一金专业计算器，让计算变得精准</text>
                <text class="to">去查看>></text>
            </navigator>
            <button class="start-cal" form-type="submit">开始计算</button>
        </view>
    </form>
    <tools :title="title" :path="path"></tools>
</view>
</template>

<script>
// pages/tools/calculator/index/index.js
import util from "../../../../utils/util";
import WxValidate from "../../../../utils/WxValidate";
const app = getApp();
var cityInfo = [];
var shebaoMax = 0;
var gongjijinMax = 0;
var shebaoMin = 0;
var gongjijinMin = 0;
import tools from "../../../../component/tools/tools";

export default {
  data() {
    return {
      city: [],
      cityIndex: 0,
      type: ["职工", "居民"],
      typeIndex: 0,
      minNum: "",
      payMinNum: "",
      proportion: "",
      isPayMinNum: false,
      isMinNum: false,
      shebaoMin: 0,
      shebaoMax: 0,
      gongjijinMin: 0,
      gongjijinMax: 0,
      gjjSwitch: false,
      title: "",
      path: ""
    };
  },

  components: {
    tools
  },
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.getUrl(app.globalData.url).then(res => {
      this.getCity();
    });
    this.initValidate();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    getCity() {
      util.request("zhuanti.php", {
        action: "ss_type"
      }).then(res => {
        if (res.data.message == "success") {
          let city = [];
          res.data.data.forEach(item => city.push(item.cityname));
          cityInfo = res.data.data;
          this.setData({
            city,
            type: res.data.data[0].type
          });
          this.getCityInfo();
        }
      });
    },

    citySelect(e) {
      let cityIndex = e.detail.value;
      let type = cityInfo[cityIndex].type;
      this.setData({
        type,
        cityIndex,
        typeIndex: 0
      });
      this.getCityInfo();
    },

    typeSelect(e) {
      let typeIndex = e.detail.value;
      this.setData({
        typeIndex
      });
      this.getCityInfo();
    },

    getCityInfo() {
      util.request("zhuanti.php", {
        action: "wuxianyijin",
        citycode: cityInfo[this.cityIndex].citycode,
        type: this.type[this.typeIndex]
      }).then(res => {
        if (res.data.message == "success") {
          shebaoMax = Number(res.data.data.ss.shebao.max);
          shebaoMin = Number(res.data.data.ss.shebao.min);
          gongjijinMax = Number(res.data.data.ss.gjj.max);
          gongjijinMin = Number(res.data.data.ss.gjj.min);
          this.setData({
            shebaoMin,
            shebaoMax,
            gongjijinMin,
            gongjijinMax,
            title: res.data.data.ss.title,
            path: res.data.data.ss.url
          });
        }
      });
    },

    minBase(e) {
      let type = e.currentTarget.dataset.type;
      if (type === "pay") this.setData({
        isPayMinNum: !this.isPayMinNum,
        payMinNum: gongjijinMin
      });else this.setData({
        isMinNum: !this.isMinNum,
        minNum: shebaoMin
      });
    },

    switchGjj(e) {
      this.setData({
        gjjSwitch: e.detail.value
      });
    },

    baseNumInput(e) {
      let minNum = e.detail.value;
      let type = e.currentTarget.dataset.type;

      if (type == "pay") {
        if (minNum < gongjijinMin) this.setData({
          payMinNum: gongjijinMin
        });else if (minNum > gongjijinMax) this.setData({
          payMinNum: gongjijinMax
        });
      } else if (type == "base") {
        if (minNum < shebaoMin) this.setData({
          minNum: shebaoMin
        });else if (minNum > shebaoMax) this.setData({
          minNum: shebaoMax
        });
      } else {
        if (minNum < 5) this.setData({
          proportion: 5
        });else if (minNum > 12) this.setData({
          proportion: 12
        });
      }
    },

    showWarnInfo(error) {
      uni.showModal({
        title: '提示',
        content: error.msg,
        showCancle: false
      });
    },

    initValidate() {
      const rule = {
        baseNum: {
          required: true,
          min: 2200,
          max: 31938
        }
      };
      const message = {
        baseNum: {
          required: "请输入参保基数",
          min: "请输入不小于2200的数字",
          max: "请输入不大于31938的数字"
        },
        payBaseNum: {
          required: "请输入缴存基数",
          min: "请输入不小于2200的数字",
          max: "请输入不大于31938的数字"
        },
        proportion: {
          required: "请输入缴存比例",
          min: "请输入不小于5的数字",
          max: "请输入不大于12的数字"
        }
      }; //  创建实例对象

      this.WxValidate = new WxValidate(rule, message);
    },

    submit(e) {
      let parms = e.detail.value;

      if (!this.WxValidate.checkForm(parms)) {
        const error = this.WxValidate.errorList[0];
        this.showWarnInfo(error);
        return false;
      } else {
        parms.type = this.type[parms.type];
        parms.city = cityInfo.filter(item => item.cityname == this.city[parms.city])[0].citycode;
        uni.navigateTo({
          url: `/pages/tools/calculator/result/result?computeData=${JSON.stringify(parms)}`
        });
      }
    }

  }
};
</script>
<style>
.tips {
    width: 26rpx;
    height: 20rpx;
    margin-right: 14rpx;
}

.border {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 34rpx 36rpx;
    border-bottom: 2rpx solid #F1F1F1;
}

.title {
    font-size: 32rpx;
    color: #000000;
    font-weight: 500;
}

.back {
    width: 18rpx;
    height: 29rpx;
}

.content {
    margin-right: 23rpx;
}

.left {
    display: flex;
    align-items: center;
}

.sy-box {
    margin-top: 50rpx;
}

.title-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 27rpx;
}

.title {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.title image {
    width: 16rpx;
    height: 28rpx;
    margin-right: 10rpx;
}

.title text {
    font-size: 32rpx;
    font-weight: bold;
    color: #141414;
}

.base-num {
    display: flex;
    margin-right: 19rpx;
}

switch {
    transform: scale(.8);
}

.other {
    border-top: 20rpx solid #F2F2F2;
}

.input-placeholder {
    text-align: right;
}

input {
    width: 400rpx;
    margin-right: 19rpx;
    text-align: right;
}

.control {
    padding: 35rpx 36rpx;
    background-color: rgb(242, 242, 242);
}

.to-major {
    height: 60rpx;
    border-radius: 30rpx;
    line-height: 60rpx;
    font-size: 22rpx;
    color: #000000;
    text-align: center;
    background-color: #F5E2CF;
}

.to {
    margin-left: 23rpx;
    color: #FF6600;
}

.start-cal {
    height: 80rpx;
    margin-top: 32rpx;
    margin-bottom: 20rpx;
    font-size: 34rpx;
    line-height: 80rpx;
    color: #FFFFFF;
    text-align: center;
    border-radius: 15rpx;
    background-color: #FF6600;
}

.cal-func {
    padding: 33rpx 0;
    padding-left: 36rpx;
}

.swiper-item {
    position: relative;
}

.swiper-img {
    height: 276rpx;
    border-radius: 16rpx;
}

.swiper-text {
    position: absolute;
    bottom: 35rpx;
    left: 36rpx;
    color: #FFFFFF;
    font-size: 32rpx;
}

.tools {
    padding: 0 37rpx;
    margin-bottom: 40rpx;
    border-top: 20rpx solid #F2F2F2;
}

.scroll-border {
    display: flex;
    align-items: center;
    height: 265rpx;
}

.tools-item {
    width: 185rpx;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0 23rpx;
    padding-top: 59rpx;
    padding-bottom: 39rpx;
    margin-top: 20rpx;
    margin-left: 37rpx;
    box-shadow: 0px 0px 14rpx 0px rgba(4, 0, 0, 0.2);
    border-radius: 10rpx;
    background-color: #FFFFFF;
    box-sizing: border-box;
}

.tools-item:first-child {
    margin-left: 14rpx;
}

.special {
    position: absolute;
    top: -16rpx;
}

.special image {
    width: 61rpx;
    height: 48rpx;
}

.icon {
    width: 74rpx;
    height: 74rpx;
}

.name {
    margin-top: 40rpx;
    font-size: 26rpx;
    color: #000000;
}

.desc {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    word-break: break-all;
    margin-top: 23rpx;
    font-size: 20rpx;
    color: #999999;
}

.bottom {
    height: 166rpx;
    background-color: #F2F2F2;
}
</style>